package com.hxzy.controller.mobile.cart;

import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.mobile.cart.req.CartAddReq;
import com.hxzy.controller.mobile.cart.resp.MobileAddCartNumReq;
import com.hxzy.controller.mobile.cart.resp.MobileCartResp;
import com.hxzy.controller.mobile.login.resp.MobileLoginResp;
import com.hxzy.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/20 14:47
 * @Version 1.0
 */
@Api(tags = "手机端-购物车")
@RestController
@RequestMapping(value = "/mobile/cart")
public class MobileCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation(value = "查询当前用户该商家的购物车")
    @GetMapping(value = "/merchantId/{id}")
    public R<List<MobileCartResp>> findByMerchantId(@PathVariable(value = "id") Long merchantId){
         List<MobileCartResp> list=this.shoppingCartService.findByMerchantId(merchantId);
         return R.okHasData(list);
    }


    @ApiOperation(value = "首次产品加入购物车")
    @PostMapping(value = "/add")
    public R cartAdd(@RequestBody @Valid CartAddReq req){
        Integer num=this.shoppingCartService.cartAdd(req);
        //创建一个map
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number", num);
        return  R.okHasData(dataMap);
    }

    @ApiOperation(value = "向已有购物车商品增加数量")
    @PostMapping(value = "/addnum")
    public R addCarNumApi(@RequestBody @Valid MobileAddCartNumReq req){
        Integer num=this.shoppingCartService.addCarNumApi(req);
        //创建一个map
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number", num);
        return  R.okHasData(dataMap);
    }

    @ApiOperation(value = "向已有购物车中减少商品数量")
    @PostMapping(value = "/sub")
    public R subCartNum(@RequestBody @Valid MobileAddCartNumReq req){
        Integer num=this.shoppingCartService.subCarNum(req);
        //创建一个map
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number", num);
        return  R.okHasData(dataMap);
    }

    @ApiOperation(value = "清空自己某个商家的购物车数据")
    @DeleteMapping(value = "/clean/{merchantId}")
    public R clearCart(@PathVariable(value = "merchantId") Long merchantId){
        boolean result=this.shoppingCartService.cleanCart(merchantId);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }


}
